package com.hongdee.insurance.quotation.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.insurance.car.entity.TInsuranceCar;
import com.hongdee.insurance.quotation.entity.TInsuranceQuotation;
import com.hongdee.insurance.quotation.service.ITInsuranceTaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 王帅
 * @className ${CLASS_NAME}
 * @type
 * @description
 * @path com.hongdee.crm.insurance.controller
 * @create 2017/6/26 10:22
 **/
@RestController
@RequestMapping("/insurance/task")
public class TInsuranceTaskController {

    @Autowired
    private ITInsuranceTaskService insuranceTaskService;

    /**
     * 显示最近的保险信息
     * @param uid
     * @param token
     * @param vin
     * @return
     */
    @RequestMapping("/latelyInsurance")
    @ResponseBody
    public ResultEntity latelyInsurance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        if("".equals(vin)){
            throw new Exception("未提供车辆信息!!");
        }
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setMsg("查询成功");
        Map info=this.insuranceTaskService.getLatelyInsurance(vin,uid);
        res.setData(info);
        return res;
    }

    @RequestMapping("/laetlyQuotation")
    @ResponseBody
    public ResultEntity laetlyQuotation(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "vin", defaultValue = "") String vin) throws Exception {
        if("".equals(vin)){
            throw new Exception("未提供车辆信息!!");
        }
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setMsg("查询成功");
        Map info=this.insuranceTaskService.getLastQuotationInfo(vin,uid);
        List<Map> list=new ArrayList<>();
        list.add(info);
        res.setData(list);
        return res;
    }

    /**
     * 最近的报价单
     * @param uid
     * @param token
     * @param vin
     * @return
     * @throws Exception
     */
    @RequestMapping("/laetlyQuotation/more")
    @ResponseBody
    public Map<String,Object> laetlyQuotationMore(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "vin", defaultValue = "") String vin,
                                        @RequestParam(name = "start", defaultValue = "0") String start,
                                        @RequestParam(name = "limit", defaultValue = "50") String limit) throws Exception {
        if("".equals(vin)){
            throw new Exception("未提供车辆信息!!");
        }
        Map<String,Object> map=this.insuranceTaskService.getLastQuotationMore(vin,uid,"",start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }


    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "taskId", defaultValue = "") String taskId,
                             @RequestBody Map data) throws Exception {
        //获得车辆信息TInsuranceCar
        TInsuranceCar insuranceCar= null;
        if(data.get("insuranceCar")!=null){
            insuranceCar= JSON.toJavaObject((JSON) JSONArray.toJSON(data.get("insuranceCar")),TInsuranceCar.class);
        }else{
            throw new Exception("信息错误，未检索到车辆信息请检查录入的信息!!");
        }
        //报价单信息
        TInsuranceQuotation insuranceQuotation= null;
        if(data.get("paramQuotation")!=null){
            insuranceQuotation=JSON.toJavaObject((JSON) JSONArray.toJSON(data.get("paramQuotation")),TInsuranceQuotation.class);
            insuranceQuotation.setStoreChanel("1");
        }else{
            throw new Exception("信息错误，未检索到报价单信息请检查录入的信息!!");
        }
        List<Map<String,Object>> itemList=null;
        if(data.get("itemList")!=null){
            itemList= (List<Map<String,Object>>) data.get("itemList");
        }else{
            throw new Exception("未录入保险项目信息，请至少录入交强或商业险!!");
        }
        this.insuranceTaskService.saveInsuranceByTask(insuranceCar,insuranceQuotation,itemList,uid,taskId);
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setMsg("报价单录入成功!!");
        return res;
    }

}
